home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-02-07 | 4.4 KB | 152 lines | [TEXT/MPS ] |
- (*
- CTBSendBytes byteList[,eom] -- Send each byte in the list out the current connection. If the eom
- parameter is present and non-empty, then set the end-of-message flag.
-
- To compile and link this file using Macintosh Programmer's Workshop,
-
- pascal -w CTBSendBytes.p
- link -m ENTRYPOINT -o HyperCommands -rt XCMD=2763 -sn Main=CTBSendBytes ∂
- CTBSendBytes.p.o "{MPW}"Libraries:interface.o "{MPW}"Libraries:Libraries:HyperXLib.o
-
- © Copyright 1990 by Apple Computer, Inc.
-
- Initial coding 2/90 by Harry R. Chesley.
- *)
-
- {$R-}
-
- {$S CTBSendBytes } { Segment name must be the same as the command name. }
-
- unit DummyUnit;
-
- interface
-
- uses MemTypes, QuickDraw, OSIntf, ToolIntf, CTBUtils, FTIntf, CMIntf, TMIntf, CRMIntf, HyperXCmd;
-
- procedure EntryPoint(paramPtr: XCmdPtr);
-
- implementation
-
- const
-
- space = ord(' '); { Space. }
- comma = ord(','); { Comma. }
- zero = ord('0'); { Zero. }
- nine = ord('9'); { Nine. }
- lowera = ord('a'); { Lower-case a. }
- lowerf = ord('f'); { Lower-case f. }
- upperA = ord('A'); { Upper-case A. }
- upperF = ord('F'); { Upper-case F. }
- dollar = ord('$'); { Dollar sign. }
-
- procedure CTBSendBytes(paramPtr: XCmdPtr); forward;
-
- procedure EntryPoint(paramPtr: XCmdPtr);
-
- begin
- CTBSendBytes(paramPtr);
- end;
-
- procedure CTBSendBytes(paramPtr: XCmdPtr);
-
- {$I CTBUtil.inc}
-
- var i: integer;
- ioBuffer: Ptr;
- ioSize: longInt;
- err: CMErr;
- h: Handle;
- flags: integer;
-
- procedure Fail(errMsg: Str255); { set theResult and quit }
- begin
- paramPtr^.returnValue := PasToZero(paramPtr,errMsg);
- exit(CTBSendBytes);
- end;
-
- function copyAndCount(copyFrom, copyTo: Ptr; doTheCopy: boolean): longInt;
- { Copy and translate bytes from the source string to the destination, counting the size of the destination
- as we go. If doTheCopy is not true, then don't actually do the copy, just accumulate the count. }
-
- var fromPtr, toPtr, nextFromPtr: Ptr;
- base, tot: integer;
- theChar: SignedByte;
-
- begin
- { Cycle thru all the bytes to be copied. }
- fromPtr := copyFrom; toPtr := copyTo;
- while fromPtr^ <> 0 do
- begin
- { Skip over leading junk. }
- while not (fromPtr^ in [0,zero..nine,lowera..lowerf,upperA..upperF,comma,dollar]) do
- fromPtr := pointer(ord4(fromPtr)+1);
- { Get the next byte. }
- base := 10;
- tot := 0;
- { If the first character is a dollar sign, interpret this item as a hex number. }
- if fromPtr^ = dollar then
- begin
- base := 16;
- while not (fromPtr^ in [0,zero..nine,lowera..lowerf,upperA..upperF,comma]) do
- fromPtr := pointer(ord4(fromPtr)+1);
- end;
- { Convert the string to a number. }
- while fromPtr^ in [zero..nine,lowera..lowerf,upperA..upperF] do
- begin
- theChar := fromPtr^;
- if theChar in [zero..nine] then tot := base*tot + theChar - zero
- else if theChar in [lowera..lowerf] then tot := base*tot + theChar - lowera + 10
- else tot := base*tot + theChar - upperA + 10;
- fromPtr := pointer(ord4(fromPtr)+1);
- end;
- { Get it as a byte. }
- theChar := SignedByte(tot);
- { Skip to the next comma. }
- while not (fromPtr^ in [0,comma]) do fromPtr := pointer(ord4(fromPtr)+1);
- { Skip past the comma. }
- if fromPtr^ <> 0 then fromPtr := pointer(ord4(fromPtr)+1);
-
- { If we're copying, copy. }
- if doTheCopy then toPtr^ := theChar;
- toPtr := pointer(ord4(toPtr)+1);
- end;
-
- { Compute the final size of the output. }
- copyAndCount := ord4(toPtr) - ord4(copyTo);
- end;
-
- begin
- { Check the parameter count. }
- i := paramPtr^.paramCount;
- if (i = 0) or (i > 2) then Fail('Invalid parameter count');
-
- { Check for an empty string being sent. }
- if not ParmPresent(1) then exit(CTBSendBytes);
- h := paramPtr^.params[1];
-
- { Make sure the Comm Toolbox is here. }
- CTBReady;
- { And so is a connection tool. }
- EnsurePresent(connectionTool);
- { And it's open. }
- EnsureOpen;
-
- { Decide if we should set the EOM flag. }
- if ParmPresent(2) then flags := cmFlagsEOM
- else flags := 0;
-
- { Figure out the size of the new buffer and allocate it. }
- ioBuffer := NewPtr(copyAndCount(h^,nil,false));
- if ioBuffer = nil then Fail('Could not allocate buffer');
-
- { Copy the data into the buffer, and set the output request size. }
- ioSize := copyAndCount(h^,ioBuffer,true);
-
- { Send the data to the port asynchronously. }
- err := CMWrite(Globals^^.connHand,ioBuffer,ioSize,cmData,false,nil,-1,flags);
- DisposPtr(ioBuffer);
- if err <> noErr then Fail('Write failed');
- end;
-
- end.
-